package com.wwg.myapplication;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.BroadcastReceiver;
import android.os.IBinder;
import android.os.SystemProperties;
import android.util.Log;

public class BurnProtectService extends Service {

    private static final String TAG = "BurnProtectService";
    private static final String ACTION_UPDATE_OPERATION = "com.skg.update_operation.action";
    private static final String ACTION_UPDATE_SYSTEM_OPERATION = "com.skg.update_operation.system.action";

    private Thread mThread;
    private Context mContext;

    // 润屏时间设置
    public static long AUTI_BURN_DELAY = 30*60;
    private long currentSecond = 0;
    private boolean isStop = false;

    // 调试用时间设置
    private static final int timeSet[] = new int[]{0, 10, 4*60, 6*60};
    
    boolean isRegister;

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "onCreate: ");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "onStartCommand: ");
        String protectBurnStatus = SystemProperties.get("persist.sys.skg.protect_burn","1");
        Log.e(TAG, "protectBurnStatus: " + protectBurnStatus);
        
        IntentFilter mFilter = new IntentFilter();
        mFilter.addAction(ACTION_UPDATE_OPERATION);
        mFilter.addAction(ACTION_UPDATE_SYSTEM_OPERATION);
        registerReceiver(mReceiver, mFilter);
        isRegister = true;
        
        AUTI_BURN_DELAY = timeSet[Integer.parseInt(protectBurnStatus)];
        isStop = false;
        
        if (mThread == null) {
            mThread = new Thread(new Runnable() {
                @Override
                public void run() {
                    while (!isStop) {
                        currentSecond++;
                        updateTime();
                        try {
                            Thread.sleep(1000);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            });
            mThread.start();
        }
        return super.onStartCommand(intent, flags, startId);
    }

    private void updateTime() {
        if(currentSecond > AUTI_BURN_DELAY){
            currentSecond = 0;
            doAutiBurn();
        }
    }

    private void doAutiBurn() {
        if (!isStop) {
            android.util.Log.d(TAG, "doAutiBurn: sendbroadcast");
            Intent autiBurnIntent = new Intent("com.skg.system.autiburn_start");
            
            // 修复：添加受保护广播标志
            autiBurnIntent.addFlags(Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND);
            autiBurnIntent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
            
            autiBurnIntent.setPackage("com.skg.tools");
            sendBroadcast(autiBurnIntent);
        }
    }

    private BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            android.util.Log.d(TAG, "onReceive: " + action);
            updateBurnProtectStatus();
            if (action.equals(ACTION_UPDATE_OPERATION) || action.equals(ACTION_UPDATE_SYSTEM_OPERATION)) {
                currentSecond = 0;
            }
        }
    };

    private void updateBurnProtectStatus() {
        String protectBurnStatus = SystemProperties.get("persist.sys.skg.protect_burn","1");
        android.util.Log.i(TAG, "updateBurnProtectStatus: " + protectBurnStatus);
        isStop = protectBurnStatus.equals("0");
        if (!isStop) {
            try {
                int burnTimeID = Integer.parseInt(protectBurnStatus);
                Log.d(TAG, "updateBurnProtectStatus: burnTimeID = " + burnTimeID);
                AUTI_BURN_DELAY = timeSet[burnTimeID];
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mReceiver != null && isRegister) {
            unregisterReceiver(mReceiver);
            mReceiver = null;
            isRegister = false;
        }
        isStop = true;
    }
}